<template>
	<view class="container">
		<image src="/static/logo_icon.png" class="logo"></image>
		<view class="name">魔利购</view>
		<button type="default" :disabled="btnLoading" :loading="btnLoading" open-type="getUserInfo" class="btn"
			@click="login">用户一键登录</button>
			
		<view type="default"class="btn1"
			@click="backHome">返回首页</view>
	</view>
</template>

<script>
	import {
		miniProgramLogin
	} from '@/api/user.js'
	export default {
		data() {
			return {
				provider: '',
				btnLoading: false
			}
		},
		onLoad(option) {
			this.getProvider()
			if (option.form) {
				setTimeout(() => {
					uni.showToast({
						title: '登录信息已失效，请重新登录',
						icon: 'none',
						duration: 2000
					})
				}, 1000)
			}
		},
		methods: {
			getProvider() {
				const that = this
				uni.getProvider({
					service: 'oauth',
					success: (result) => {
						result.provider.map((value) => {
							that.provider = value
						})
					},
					fail: (error) => {
						console.log('获取登录通道失败', error);
					}
				});
			},
			
			backHome(){
				uni.reLaunch({
					url: '/pages/index/index'
				})
			},
			login() {
				this.btnLoading = true;
				const that = this
				if (that.provider == 'weixin') {
					const p1 = that.wxGetCode() // 把wx.login封装成promise对象
					const p2 = that.wxGetUserInfo() // 把wx.getUserProfile封装成promise对象
					Promise.all([p1, p2]).then((res) => {
						const infoRes = res[1]
						const loginRes = res[0]
						console.log(infoRes, loginRes.code)
						that.miniProgramAuthorization(infoRes, loginRes.code)
						that.btnLoading = false
					}).catch((err) => {
						console.log(err, 'failxxxxxs')
						that.btnLoading = false
					})
				} else {
					uni.login({
						provider: that.provider,
						success: function(loginRes) {
							console.log(loginRes.code);
							uni.getUserInfo({
								provider: that.provider,
								success: function(infoRes) {
									console.log(infoRes, 'infoRes')
									that.miniProgramAuthorization(infoRes, loginRes.code)
								}
							});
						},
						fail: (err) => {
							uni.showModal({
								showCancel: false,
								title: '登录失败',
								content: JSON.stringify(err)
							});
						},
						complete: () => {
							that.btnLoading = false
						}
					})
				}
			},
			miniProgramAuthorization(res, jsCode) {
				console.log(res)
				const data = {
					'encryptedData': res.encryptedData,
					'iv': res.iv,
					'jsCode': jsCode
				}
				miniProgramLogin(data).then((respanse) => {
					uni.setStorageSync('token', respanse.result.token)
					uni.setStorageSync('session_key', respanse.result.sessionKey)
					this.$store.dispatch('user/getUserInfo')
					if (uni.getStorageSync("inTell") == "1") {
						uni.setStorageSync("inTell", "3");
						uni.reLaunch({
							url: '/pages/store/index?shopId=' + uni.getStorageSync("inTellShop")
						})
						return
					} else if (uni.getStorageSync("inTell") == "2") {
						uni.setStorageSync("inTell", "3");
						uni.reLaunch({
							url: '/pages/online/index?equipmentNo=' + uni.getStorageSync("equipmentNo")
						})
						return
					}

					uni.reLaunch({
						url: '/pages/index/index'
					})
				})
			},

			// 微信获取用户信息
			wxGetUserInfo() {
				return new Promise((resolve, reject) => {
					uni.getUserProfile({
						//provider: that.provider,
						desc: '用于完善会员资料',
						success: function(infoRes) {
							console.log('2xxx')
							resolve(infoRes)
						},
						fail: (err) => {
							reject(err)
						}
					});
				})
			},

			// 微信获取code
			wxGetCode() {
				return new Promise((resolve, reject) => {
					const that = this
					uni.login({
						provider: that.provider,
						success: function(loginRes) {
							console.log('1xxx')
							resolve(loginRes)
						},
						fail: (err) => {
							reject(err)
						}
					})
				})
			}
		}
	}
</script>
<style>
	body {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.logo {
		display: block;
		margin: 191rpx auto 0;
		width: 178rpx;
		height: 178rpx;
		border-radius: 16rpx;
	}

	.name {
		margin-top: 69rpx;
		line-height: 50rpx;
		text-align: center;
		font-size: 40rpx;
		color: #040000;
	}

	.btn {
		display: block;
		margin: 160rpx auto 0;
		width: 678rpx;
		height: 97rpx;
		font-size: 36rpx;
		color: #fff;
		background: #62A744;
		border-radius: 10rpx;

		&[disabled] {
			color: #fff !important;
		}
	}
	
	.btn1 {
		position: absolute;
	    bottom:  60rpx;
		width: 100%;
		height: 97rpx;
		font-size: 36rpx;
		color: #666;
		text-align: center;
	
		border-radius: 10rpx;

	}
</style>
